﻿using System;
using System.Data;

namespace DNNToolkit.Common
{
    public abstract class TransDataProvider
    {
        #region "Private Fields"

        private IDbConnection mDataConnection;
        private IDbTransaction mDataTransaction;

        #endregion
        
        #region "Protected Property"

        protected IDbConnection DataConnection
        {
            get { return mDataConnection; }
            set { mDataConnection = value; }
        }

        protected IDbTransaction DataTransaction
        {
            get { return mDataTransaction; }
        }

        #endregion

        #region "Public Methods"

        public void OpenConnection()
        {
            if (mDataConnection.State != ConnectionState.Open)
                mDataConnection.Open();
        }

        public void CloseConnection()
        {
            if (mDataConnection.State != ConnectionState.Closed)
                mDataConnection.Close();
        }

        public void BeginTrans()
        {
            mDataTransaction = mDataConnection.BeginTransaction();
        }

        public void Commit()
        {
            if (mDataTransaction != null)
            {
                mDataTransaction.Commit();
                mDataTransaction = null;
            }
        }

        public void RollBack()
        {
            if (mDataTransaction != null)
            {
                mDataTransaction.Rollback();
                mDataTransaction = null;
            }
        }

        #endregion
    }
}
